package com.guoliang.eduService.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.commonUtils.JwtUtils;
import com.guoliang.eduService.entity.EduCourse;
import com.guoliang.eduService.entity.vo.ChapterVo;
import com.guoliang.eduService.entity.vo.CourseFrontVo;
import com.guoliang.eduService.entity.vo.CourseInfoVo;
import com.guoliang.eduService.entity.vo.CourseQuery;
import com.guoliang.eduService.service.*;
import com.guoliang.viewcount.aop.ViewCount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hamcrest.core.CombinableMatcher;
import org.junit.jupiter.params.ParameterizedTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ：Leo
 * @date ：Created in 2023/1/29 15:56
 * @description：
 */
@RestController
@RequestMapping("/eduService/coursefront")
@Api(description = "课程前端")
public class CourseFrontController {
    @Autowired
    EduCourseService courseService;
    @Autowired
    EduChapterService chapterService;
    @Autowired
    OrderClient orderClient;
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public CommonResult getFrontCourseList(@PathVariable Long page, @PathVariable Long limit
            , @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> coursePage=new Page<>(page,limit);
        Map<String,Object> map=courseService.getFrontCourseList(coursePage,courseQuery);
        return CommonResult.ok().date(map);
    }
    //自定义注解实现浏览量统计
    @ViewCount(description = "course")
    @ApiOperation("课程详情")
    @GetMapping("getFrontCourseInfo/{id}")
    public CommonResult getFrontCourseInfo(@PathVariable String id, HttpServletRequest request){
        CourseFrontVo courseInfo=courseService.getFrontCourseInfo(id);
        //根据课程id获取章节和小节信息
        List<ChapterVo> chapterVideoList = chapterService.getChapterAndVideo(id);
        //获取订单状态
        String member = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy=false;
        if (!StringUtils.isEmpty(member)) isBuy=orderClient.isBuy(id,member);
        return CommonResult.ok().data("chapterVideoList",chapterVideoList).data("course",courseInfo)
                .data("isBuy",isBuy);
    }
}
